package com.zy.oauth.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.common.core.annotate.Log;
import com.zy.common.core.enums.ErrorEnums;
import com.zy.common.core.exception.BizException;
import com.zy.oauth.dto.AccountDto;
import com.zy.oauth.dto.RolePermissionDto;
import com.zy.oauth.dto.query.UserQueryDto;
import com.zy.oauth.entity.Account;
import com.zy.oauth.entity.Password;
import com.zy.oauth.entity.RolePermission;
import com.zy.oauth.mapper.AccountMapper;
import com.zy.oauth.service.AccountService;
import com.zy.oauth.service.PasswordService;
import com.zy.oauth.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Autowired
    private PasswordService passwordService;

    @Resource
    private PermissionService permissionService;

    @Override
    public List<Account> getUserByName(String name) {
        if (StringUtils.isNotBlank(name)) {
            return null;
        }
//        UserQueryDto dto = AccountCov.INSTANCE.nameToUser(name);
//        QueryWrapper queryWrapper = QueryUtils.getParam(new QueryWrapper<Account>(), dto, UserQueryDto.class);
//        return this.list(queryWrapper);
        return null;
    }

    @Override
    public Account getUserByLoginName(String loginName) {
        if (StringUtils.isBlank(loginName)) {
            return null;
        }
        UserQueryDto dto = new UserQueryDto();
        dto.setLoginName(loginName);
        List<Account> list = baseMapper.list(dto, UserQueryDto.class);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        // 获取用户
        Account account = getUserByLoginName(name);
        AccountDto accountDto = new AccountDto();
        System.out.println("Account:" + account);
        if (account == null) {
            throw new BizException(ErrorEnums.USER_NOT_FOUND);
        }
        BeanUtils.copyProperties(account, accountDto);
        // 获取密码
        Password password = passwordService.getPassword(account.getUserId());
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(accountDto);
        // 权限集合
        List<RolePermissionDto> permissions = permissionService.getMenuPermission(roles);

        //需要构造org.springframework.security.core.userdetails.User 对象包含账号密码还有用户的角色
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        accountDto.setUsername(account.getLoginName());
        accountDto.setPassword(passwordEncoder.encode(password.getPassword()));
        accountDto.setUserId(account.getUserId());
        accountDto.setRoles(roles);
        accountDto.setPermissions(permissions);
        return accountDto;
    }

}
